home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / netconf / devlist.c < prev    next >
C/C++ Source or Header  |  1996-05-04  |  2KB  |  76 lines

  1. #include <string.h>
  2. #include "../misc/misc.h"
  3. #include "netconf.h"
  4. #include "../paths.h"
  5.  
  6. static NETCONF_HELP_FILE help_dev ("devlist");
  7.  
  8. static CONFIG_FILE f_dev (PROC_NET_DEV
  9.     ,help_dev
  10.     ,CONFIGF_PROBED);
  11.  
  12.  
  13. /*
  14.     Get the list of all network device (except aliases).
  15.     Return -1 if any errors.
  16. */    
  17. int devlist_read (SSTRINGS &list)
  18. {
  19.     int ret = -1;
  20.     FILE *fin = f_dev.fopen ("r");
  21.     if (fin != NULL){
  22.         char buf[600];
  23.         if (fgets(buf,sizeof(buf)-1,fin)!=NULL
  24.             && fgets(buf,sizeof(buf)-1,fin)!=NULL){
  25.             ret = 0;
  26.             while (fgets(buf,sizeof(buf)-1,fin)!=NULL){
  27.                 char word[600];
  28.                 str_copyword (word,buf);
  29.                 char *pt = strchr(word,':');
  30.                 if (pt != NULL){
  31.                     if (strchr(pt+1,':')==NULL){
  32.                         /* #Specification: netconf / proc/net/dev
  33.                             netconf assume that /proc/net/dev has the following
  34.                             format. 2 lines of heading followed by device info.
  35.                             Each line begin by the device name. A device name
  36.                             is ended by :, while an alias has a : in the middle
  37.                             and at the end.
  38.                         */
  39.                         *pt = '\0';
  40.                         list.add (new SSTRING(word));
  41.                     }
  42.                 }
  43.             }
  44.         }
  45.         fclose (fin);
  46.     }
  47.     return ret;
  48. }
  49.  
  50.  
  51. /*
  52.     Return != 0 if a network device is available in the kernel
  53.  
  54.     This function is implemented by reading /proc/net/dev instead of
  55.     asking the kernel directly. Asking the kernel directly has the side
  56.     effect of triggering kerneld if the device is not loaded. This is
  57.     not always what we want.
  58. */
  59. int devlist_devexist (const char *devname)
  60. {
  61.     int ret = 0;
  62.     SSTRINGS list;
  63.     if (devlist_read(list)!=-1){
  64.         int n = list.getnb();
  65.         for (int i=0; i<n; i++){
  66.             SSTRING *s = list.getitem(i);
  67.             if (s->cmp(devname)==0){
  68.                 ret = 1;
  69.                 break;
  70.             }
  71.         }
  72.     }
  73.     return ret;
  74. }
  75.  
  76.